<?php

namespace addons\shopro\controller\article;

use addons\shopro\controller\Common;
use addons\shopro\service\goods\GoodsService;
use \app\admin\model\westts\Article as ArticleModel;
use app\admin\model\westts\reporting\Record;

class Article extends Common
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ArticleModel();
    }

    /**
     * 获取软文列表
     */
    public function index()
    {
        $params = $this->request->param();
        $category = $params['category'] ?? 'news';
        $service = $this->model;

        if ($category) {
            $service->where("category", $category);
        }

        $article = $service->field("id,category,title,cover,pv,createtime")->paginate($params["list_rows"]??7);

        $this->success('获取成功', $article);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取首页软文以及对应分类
     */
    public function category(){
        $this->model->order("sort desc,createtime desc");
        $this->model->limit(0,3);
        $result=[
            "新闻"=>$this->model->where("category='news'")->field("id,category,title,cover,pv,createtime")->select(),
            "活动"=>$this->model->where("category='active'")->field("id,category,title,cover,pv,createtime")->select(),
            "公告"=>$this->model->where("category='platform'")->field("id,category,title,cover,pv,createtime")->select()
        ];
        $this->success('获取成功',$result );
    }

    /**
     * @param $id
     * 获取软文详情
     */
    public function detail(){
        $id = $this->request->get('id');
        $article = $this->model->where('id', $id)->find();
        if (!$article) {
            $this->error(__('No Results were found'));
        }
        $this->model->where('id', $id)->setInc("pv",1);
        $this->success('获取成功', $article);
    }

    /**
     * 添加记录报备记录
     */
    public function reporting(){
        $params = $this->request->param();
        $this->svalidate($params, ".create");
        $data=[
            "project_id"    =>  0,
            "peoject_name"  =>  $params["project"],
            "username"      =>  $params["name"],
            "phone"         =>  $params["tel"],
            "region"        =>  $params["area"],
            "createtime"    =>  date("Y-m-d H:i:s"),
        ];
        Record::create($data);
        $this->success('保存成功');
    }

}
